<?php

namespace App\Http\Controllers\Frontend\Auth;

use App\Http\Controllers\Frontend\FrontendController;
use App\Http\Requests\Frontend\Auth\RegisterRequest;
use App\Http\Resources\Frontend\ApiResource;
use App\Http\Resources\Frontend\UserResource;
use App\Models\User;
use Illuminate\Auth\Events\Registered;

class RegisterController extends FrontendController
{

    public function __construct()
    {
        $this->middleware('captcha.verify');
    }

    public function register(RegisterRequest $request)
    {
        event(new Registered($user = $this->create($request->all())));

        $token = auth($this->guard)->login($user);

        $user = array_merge($user->toArray(), $this->respondWithToken($token));

        return (new UserResource($user));
    }


    /**
     * @param array $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => \Hash::make($data['password']),
        ]);
    }
}
